import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/udt_tb_workreportrecord_odoo_api.dart';
import 'package:flutter_admin/models/odoo/udt_tb_workreportrecord.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'details.dart';

class ProductWorkreportrecordScreen extends StatefulWidget{

  ProductWorkreportrecordScreen({Key? key}): super(key: key);

  @override
  _ProductWorkreportrecordScreenState createState()  => _ProductWorkreportrecordScreenState();

}

class _ProductWorkreportrecordScreenState extends State<ProductWorkreportrecordScreen>{

  ScrollController _controller = ScrollController();

  late Timer _timer;

  double _currentPixels = 0;

  List _dataList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _loadData();
    _timer = Timer.periodic(Duration(seconds: 3), (Timer t) => autoScroll());
  }

  @override
  void dispose() {
    //为了避免内存泄露，需要调用_controller.dispose
    _controller.dispose();
    _timer.cancel();
    super.dispose();
  }

  autoScroll(){
    if (_controller.position.pixels ==
        _controller.position.maxScrollExtent) {
      _currentPixels = 0;
    }else{
      _currentPixels += 300;
    }
    _controller.animateTo(
      _currentPixels,
      duration: Duration(milliseconds: 1300),
      curve: Curves.ease,
    );
  }

  _loadData() async{
    ResponseBodyApi res = await UdtTbWorkreportrecordOdooApi.findScreen();
    if(res.success!){
      _dataList = res.data;
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> list = _dataList.map((e) => ProductWorkreportrecordScreenDetial(
      title: e["station"]["name"], monthlyPay: e["monthly_pay"],
      dailyOutput: e["daily_output"],monthlyOutput: e["monthly_output"],
    )).toList();
    // TODO: implement build
    return Container(
      alignment: Alignment.center,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage("assets/bg/bg5.jpg"),
          fit: BoxFit.cover,
        ),
      ),
      child: Column(
        // mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Padding(
            padding: EdgeInsets.all(10.r),
            child: Text("设备工位报工产量统计",style: TextStyle(
              fontSize: 36.sp,
              decoration: TextDecoration.none,
              color: Color.fromRGBO(255, 255, 255, 1)
            ),),
          ),
          Expanded(child: GridView(
              controller: _controller,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4, //横轴三个子widget
                childAspectRatio: 2.26, //宽高比为2
                mainAxisSpacing: 2.0,
                crossAxisSpacing: 2.0,
              ),
              children:list
            ),
          )
        ],
      ),
    );
  }
}